// pages/authentica/timeauthentica.js
import { areaList } from '~/miniprogram_npm/@vant/area-data/index';
import config from '~/configs/configs'
import baseuser from '~/utils/users.js'
import basesup from '~/utils/supplier.js'
import baseteam from '~/utils/teamleader.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        autosize: { maxHeight: 100, minHeight: 90 },
        changestart: 1,
        show: false,

        // 公司类型
        companyTypelist: [],
        companychage: false,
        allcompanyselect: '',
        // 类型
        // 核心
        hexinList: [],
        // 核心
        // 营业
        yingyeList: [],
        // 营业
        // 经营 
        jingyingList: [],
        // 经营 

        // 店铺主营类目
        zhiying: false,
        zhiyingcolumns: ['暂无数据'],
        zhiyingselect: '',

        companyName: '',
        companyType: '',
        areaList,
        changeareadata:[],
        companyArea: '',
        companyTouchPerson: '',
        companyTouchPhone: '',
        companyIntro: '',
        companyProductIntro: '',
        companyCooperativeChannel:'',

        storeLogo:'',
        storeName:'',
        storeMainCategory:'',
        storeMainProduct:''
    },
    nextbacknext() {
        this.setData({
            changestart: 1
        })
    },
    companychagetype() {
        this.setData({
            companychage: true
        })
    },
    companyonClose() {
        this.setData({
            companychage: false,
            zhiying:false,
            shengshi:false
        })
    },
    alertzhuying(){
        this.setData({
            zhiying:true,
        })
    },
    async getzhuying() {
        let reqdata = await baseteam.zhuyinglist()
        let zhiyingcolumns = reqdata.data.result
        this.setData({
            zhiyingcolumns
        })
    },
    onConfirmzhiying(event) {
        let { picker, value, index } = event.detail;
        console.log(picker, value, index)
        this.setData({
            zhiyingselect: index
        })
        setTimeout(() => {
            this.companyonClose()
        }, 160);
    },
    companyTypeselect(items) {
        let selectlength = 0
        let maxlength = 2
        let setdatas = 0
        console.log(this.data.companyTypelist[items.currentTarget.dataset.selectindex].selects)
        if (this.data.companyTypelist[items.currentTarget.dataset.selectindex].selects == 0 || this.data.companyTypelist[items.currentTarget.dataset.selectindex].selects == undefined || this.data.companyTypelist[items.currentTarget.dataset.selectindex].selects == null) {
            for (const istwo in this.data.companyTypelist) {
                if (this.data.companyTypelist[istwo].selects == 1) {
                    selectlength += 1
                    if (selectlength >= maxlength) {
                        wx.showToast({
                            title: '已经选中了两个',
                            icon: 'none',
                            duration: 2000
                        })
                        return false
                    }
                }
            }
            setdatas = 1
        }


        let setstring = `companyTypelist[${items.currentTarget.dataset.selectindex}].selects`
        this.setData({
            [setstring]: setdatas
        })
        let allcompany = ''
        for (const isf in this.data.companyTypelist) {
            if (this.data.companyTypelist[isf].selects == 1) {
                allcompany = allcompany + this.data.companyTypelist[isf].name + '，'
            }
        }
        allcompany = allcompany.substring(0, allcompany.length - 1)
        this.setData({
            allcompanyselect: allcompany
        })
    },
    async getcompanyType() {
        let resdata = await basesup.companyType()
        this.setData({
            companyTypelist: resdata.data.result
        })
    },
    // 核心
    bangafterReadhexin(event) {
        const { file } = event.detail;
        const { hexinList = [] } = this.data;
        hexinList.push(...file);
        this.setData({ hexinList });
    },
    hexindel(event) {
        let hexinList = this.data.hexinList;
        hexinList.splice(event.detail.index, 1)
        this.setData({ hexinList });
    },
    // 营业
    // yingyeList
    bangafterReadyingye(event) {
        const { file } = event.detail;
        const { yingyeList = [] } = this.data;
        yingyeList.push(...file);
        this.setData({ yingyeList });
    },
    yingyedel(event) {
        let yingyeList = this.data.yingyeList;
        yingyeList.splice(event.detail.index, 1)
        this.setData({ yingyeList });
    },
    // 经营
    // jingyingList
    bangafterReadjingying(event) {
        const { file } = event.detail;
        const { jingyingList = [] } = this.data;
        jingyingList.push(...file);
        this.setData({ jingyingList });
    },
    jingyingdel(event) {
        let jingyingList = this.data.jingyingList;
        jingyingList.splice(event.detail.index, 1)
        this.setData({ jingyingList });
    },
    // logo
    bangafterReadlogo(event) {
        const { file } = event.detail;
        console.log(file)
        wx.uploadFile({
            url: config.baseUrl + baseuser.adminossfileupload,
            filePath: file.url,
            name: 'file',
            timeout: 60 * 1000,
            success: function (res) {
                console.log(res)
            },
            complete: (datas) => {
                let data = JSON.parse(datas.data);
                if (data.code == 200) {
                    this.setData({
                        storeLogo: data.data.map.url
                    })
                } else {
                    wx.showToast({
                        title: '上传失败，请重试',
                        icon: 'error',
                        duration: 2000
                    })
                }
            }
        });
    },

    showPopup() {
        this.setData({ show: true });
    },

    onClose() {
        this.setData({ show: false });
    },
    companychageArea(){
        this.setData({
            shengshi:true
        })
    },
    shengshiconfirm(item) {
        let companyArea = ''
        for (const i in item.detail.values) {
            companyArea = companyArea + item.detail.values[i].name
        }
        this.setData({
            changeareadata: item.detail.values,
            companyArea: companyArea
        });
        this.companyonClose()
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getcompanyType()
        this.getzhuying()
    },
    
    nextnave() {
        // companyName: '',
        // companyType: '',
        // companyArea: '',
        // companyTouchPerson: '',
        // companyTouchPhone: '',
        // companyIntro: '',
        // companyProductIntro: '',
        // companyCooperativeChannel:'',
        if (this.data.companyName == '' || this.data.companyName == null || this.data.companyName == undefined) {
            wx.showToast({
                title: '公司全称还未输入',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.allcompanyselect == '' || this.data.allcompanyselect == null || this.data.allcompanyselect == undefined) {
            wx.showToast({
                title: '公司类型未选择',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.companyArea == '' || this.data.companyArea == null || this.data.companyArea == undefined) {
            wx.showToast({
                title: '公司位置未输入',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.companyTouchPerson == '' || this.data.companyTouchPerson == null || this.data.companyTouchPerson == undefined) {
            wx.showToast({
                title: '公司联系人未输入',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.companyTouchPhone == '' || this.data.companyTouchPhone == null || this.data.companyTouchPhone == undefined) {
            wx.showToast({
                title: '公司联系方式未输入',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.companyIntro == '' || this.data.companyIntro == null || this.data.companyIntro == undefined) {
            wx.showToast({
                title: '公司简介未输入',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.companyIntro == '' || this.data.companyIntro == null || this.data.companyIntro == undefined) {
            wx.showToast({
                title: '公司简介未输入',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.yingyeList.length == 0) {
            wx.showToast({
                title: '营业执照未上传',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.jingyingList.length == 0) {
            wx.showToast({
                title: '经营许可未上传',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        

        this.setData({
            changestart: 2
        })
    },

    async addcompany(){
        if (this.data.storeLogo == ''){
            wx.showToast({
                title: '未上传logo',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.storeName == ''){
            wx.showToast({
                title: '未填写店铺名称',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.zhiyingselect == ''){
            wx.showToast({
                title: '未选择主营类目',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.companyArea == ''){
            wx.showToast({
                title: '未选择地区',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.storeName == ''){
            wx.showToast({
                title: '未填写主营产品',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        wx.showLoading({
            title: '上传中, 请稍等..',
            mask: true
        })

        let hexinchanpinlist = []
        if (this.data.hexinList.length != 0) {
            hexinchanpinlist = await this.filesUpload(this.data.hexinList)
        }
        let yingyezhizhaolist = []
        if (this.data.yingyeList.length != 0) {
            yingyezhizhaolist = await this.filesUpload(this.data.yingyeList)
        }
        let jingyingxukelist = []
        if (this.data.jingyingList.length != 0) {
            jingyingxukelist = await this.filesUpload(this.data.jingyingList)
        }
        console.log(hexinchanpinlist)
        console.log(yingyezhizhaolist)
        console.log(jingyingxukelist)
        
        
        let reqdata = {
            "companyName": this.data.companyName,
            "companyType": this.data.allcompanyselect,
            "companyArea": this.data.companyArea,
            "companyAreaCity":this.data.changeareadata[0].name,
            "companyTouchPerson": this.data.companyTouchPerson,
            "companyTouchPhone": this.data.companyTouchPhone,
            "companyIntro": this.data.companyIntro,
            "companyProductIntro": this.data.companyProductIntro,
            "companyCooperativeChannel": this.data.companyCooperativeChannel,
            // 图片
            // "companyProductLogo":hexinchanpinlist,
            "msCompanyCertificateList":hexinchanpinlist,
            // "companyBusinessLicense": yingyezhizhaolist,
            "companyBusinessLicenseList":yingyezhizhaolist,
            // "companyCertificatePicture":jingyingxukelist,
            "msCompanyProductList":jingyingxukelist,

            "msCompanyStore":{
                "storeLogo":this.data.storeLogo,
                "storeName":this.data.storeName,
                "storeMainCategory":this.data.zhiyingcolumns[this.data.zhiyingselect].name,
                "storeMainProduct":this.data.storeMainProduct
            },
        }
        let resdata = await baseuser.applycompany(reqdata)
        if (resdata.code == 200) {
            wx.hideLoading()
            wx.showToast({
                title: '上传成功',
                icon: 'success',
                duration: 2000
            })
            this.setData({
                show:true
            })

        } else {
            wx.hideLoading()
            wx.showToast({
                title: '上传失败',
                icon: 'error',
                duration: 2000
            })
        }
    },

    filesUpload(paths) {
        return new Promise((resolve, reject) => {
            // wx.showLoading({
            //     title: '上传中, 请稍等..',
            //     mask: true
            // })
            let uploadCount = 0; // 上传计数
            const resources = [] // 上传结果
            //上传图片
            for (const path of (Array.isArray(paths) ? paths : [paths])) {
                wx.uploadFile({
                    url: config.baseUrl + baseuser.adminossfileupload,
                    filePath: path.url,
                    name: 'file',
                    timeout: 60 * 1000,
                    success: function (res) {
                        let data = JSON.parse(res.data);
                        if (data.code == 200) {
                            resources.push(data.data.map.url)
                        }
                    },
                    complete: () => {
                        uploadCount++
                        if (resources.length == (Array.isArray(paths) ? paths.length : 1)) {
                            // wx.hideLoading()
                            if (resources.length == 0) {
                                // showToast('上传失败')
                                reject('上传失败')
                            } else {
                                // showToast(paths instanceof String ? '上传成功' : '成功上传：' + resources.length + '张')
                                resolve(resources)
                            }
                        }
                    }
                });
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})